package org.haredot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.haredot.entity.Btn;
import org.haredot.entity.Func;
import org.haredot.mapper.BtnMapper;
import org.haredot.service.BtnService;
import org.haredot.vo.PageVo;
import org.haredot.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class BtnServiceImpl implements BtnService {

    @Resource
    private BtnMapper btnMapper ;

    @Override
    public ResultVo<?> saveBtn(Long funcId, Btn btn) {
        Func func = new Func()  ;
        func.setId(funcId);
        btn.setFunc(func);

        // 查看 权限标记，不能重复
        Btn b = btnMapper.findBtnByRemark(btn.getRemark()) ;

        if (b !=null) {
            return ResultVo.fail("权限标记已存在").build();
        }
        // 直接添加按钮即可
        int len = btnMapper.saveBtn(btn);
        return ResultVo.asserts(len == 1, "添加数据失败").data(btn).build();
    }

    @Override
    public ResultVo<?> queryBtnsByFuncId(Long funcId, PageVo<Btn> pageVo) {


        PageInfo<Btn> info = PageHelper.startPage(pageVo)
                .doSelectPageInfo(() -> btnMapper.findByFuncId(funcId));

        pageVo.setList(info.getList()).setTotal(info.getTotal()) ;

        ResultVo<PageVo<Btn>> resultVo = ResultVo.<PageVo<Btn>>ok().data(pageVo).build();

        return resultVo ;
    }

    @Override
    public ResultVo<?> modifyStatus(Map<String, ? extends Number> stringHashMap) {

        int len = btnMapper.modifyStatusById(stringHashMap) ;

        return ResultVo.asserts(len ==1 , "更新状态失败").build();
    }

    @Override
    public ResultVo<?> removeBtn(Long btnId) {
        int len = btnMapper.removeBtnById(btnId);
        return ResultVo.asserts(len == 1, "数据移除失败").build();
    }
}
